package com.iqiyi.pps.epg.core.rpc.record;

import com.iqiyi.pps.epg.api.model.web.record.TRecordColData;
import com.iqiyi.pps.epg.api.model.web.record.TRecordColDataPage;
import com.iqiyi.pps.epg.api.server.web.record.RecordColRpcService;
import com.iqiyi.pps.epg.core.service.channel.ChannelService;
import com.iqiyi.pps.epg.core.service.record.IRecordAsyncService;
import com.iqiyi.pps.epg.core.service.record.RecordAsyncServiceImpl;
import com.iqiyi.pps.epg.core.service.record.RecordColService;
import com.iqiyi.pps.epg.core.service.record.RecordColServiceImpl;
import com.iqiyi.pps.epg.core.utils.ApplicationContextUtil;
import com.iqiyi.pps.epg.core.utils.Constants;
import com.iqiyi.pps.epg.core.utils.WriteSystemLogUtil;
import org.apache.thrift.TException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by wangle on 2018/4/10.
 */
@Component
public class RecordColRpcServiceImpl implements RecordColRpcService.Iface {
    private static final Logger logger = LoggerFactory.getLogger(RecordColRpcServiceImpl.class);
    private IRecordAsyncService getRecordAsyncService() {
        return WriteSystemLogUtil.getInstance().getRecordAsyncService();
    }

    @Override
    public String save(String param) throws TException {
        String code = null;
        try {
            RecordColService recordColService = (RecordColService) ApplicationContextUtil.getBean(RecordColService.class);
            logger.info("[RecordColRpcServiceImpl][save][start][param={}]", param);
            recordColService.save(param);

            logger.info("[RecordColRpcServiceImpl][save][finish][param={}]", param);
            return code = Constants.CODE_SUC;
        } catch (Exception e) {
            logger.info("[RecordColRpcServiceImpl][save][finish][param={}]", param);
            return code = Constants.CODE_ERROR;
        }
    }

    @Override
    public String update(String param) throws TException {
        String code = null;
        try {
            RecordColService recordColService = (RecordColService) ApplicationContextUtil.getBean(RecordColService.class);
            logger.info("[RecordColRpcServiceImpl][update][start][param={}]", param);
            recordColService.update(param);
            logger.info("[RecordColRpcServiceImpl][update][finish][param={}]", param);
            return code = Constants.CODE_SUC;
        } catch (Exception e) {
            logger.info("[RecordColRpcServiceImpl][update][finish][param={}]", param);
            return code = Constants.CODE_ERROR;
        }
    }

    @Override
    public TRecordColDataPage getRecordCol(com.iqiyi.pps.epg.api.model.web.query.TPage page, com.iqiyi.pps.epg.api.model.web.query.TFilter filter) throws TException {
        TRecordColDataPage code = null;
        try {
            RecordColService recordColService = (RecordColService) ApplicationContextUtil.getBean(RecordColService.class);
            logger.info("[RecordColRpcServiceImpl][getRecordCol][start][page={}]", page);
            TRecordColDataPage recordCol = recordColService.getRecordCol(page, filter);

            logger.info("[RecordColRpcServiceImpl][getRecordCol][finish][page={}]", page);
            return recordCol;
        } catch (Exception e) {
            logger.info("[RecordColRpcServiceImpl][getRecordCol][finish][page={}]", page);
            return null;
        }
    }

    @Override
    public String deleteRecordCol(long id) throws TException {
        String code = null;
        try {
            RecordColService recordColService = (RecordColService) ApplicationContextUtil.getBean(RecordColService.class);
            logger.info("[RecordColRpcServiceImpl][deleteRecordCol][start][id={}]", id);
            recordColService.deleteRecordCol(id);
            getRecordAsyncService().addRecordPlanClearByCol(id);
            logger.info("[RecordColRpcServiceImpl][deleteRecordCol][finish][id={}]", id);
            return code = Constants.CODE_SUC;
        } catch (Exception e) {
            logger.info("[RecordColRpcServiceImpl][deleteRecordCol][finish][id={}]", id);
            return code = Constants.CODE_ERROR;
        }
    }

    @Override
    public TRecordColData getRecordColInfo(long id) throws TException {
        String code = null;
        TRecordColData tRecordColData = null;
        try {
            RecordColService recordColService = (RecordColService) ApplicationContextUtil.getBean(RecordColService.class);
            logger.info("[RecordColRpcServiceImpl][getRecordColInfo][start][id={}]", id);
            tRecordColData = recordColService.recordColInfo(id);
            logger.info("[RecordColRpcServiceImpl][getRecordColInfo][finish][id={}]", id);
            return tRecordColData;
        } catch (Exception e) {
            logger.info("[RecordColRpcServiceImpl][getRecordColInfo][error][id={}]", id);
        }
        return tRecordColData;
    }
}
